package com.raccoonagents.tools.cascet2d.model.particle;

import org.lwjgl.opengl.GL11;

import com.raccoonagents.tools.cascet2d.interfaces.rendering.ITexturedRenderable;
import com.raccoonagents.tools.cascet2d.model.Vector2f;


public class TexturedParticle extends Particle implements ITexturedRenderable{
	protected int texture;
	
	public TexturedParticle(final float [] color, final int texture) {
		super();
		setColor(color);
		this.texture = texture;
		init();
	}

	public TexturedParticle(final float [] color, final int texture, final Vector2f size){
		super();
		setColor(color);
		this.texture = texture;
		init();		
		setSize(size);
	}

	public TexturedParticle(final float [] color, final int texture, final Vector2f size, final long minLife, final long maxLife){
		super();
		setColor(color);
		this.texture = texture;
		setSize(size);
		opacity = 0.1f;
		this.minLife = minLife;
		this.maxLife = (long) (Math.random() * maxLife) + minLife;
		
		this.currentLife = this.maxLife;		
		//speed = (float) Math.random() / 10;
	}
	
	
	private void init(){
		opacity = 0.1f;
		maxLife = (long) (Math.random() * 25000);
		currentLife = maxLife;
		//speed = (float) Math.random() / 100;
		setSize(new Vector2f(40,40));
		
	}
		
	@Override
	public Particle copy(final ParticleSystem system) {
		final TexturedParticle copy = new TexturedParticle(color,texture,size, this.minLife, this.maxLife);
		//final int sz = (int) (Math.random() * 160);
		//setSize(new Vector2f(sz,sz));
		copy.setSystem(system);
		copy.setSpeed(system.getNewSpeed());
		if(system != null){			
			final int sz = (int) (Math.random() * system.particleSizeVariance) + system.particleMinSize;
			copy.setSize(new Vector2f(sz,sz));
		}
		return copy;
	}


	@Override
	public void update(final long time) {
		super.update(time);
		opacity = (float)currentLife / (float)maxLife;		
		
	}
	@Override
	public void render() {
		final ITexturedRenderable r = this;
		final Vector2f size = r.getSize();
		// GL11.glLoadIdentity();
		GL11.glPushMatrix();
		
		final Vector2f position = r.getLocation();
		
		GL11.glTranslatef(position.x, position.y, 0.0f);
		GL11.glEnable(GL11.GL_TEXTURE_2D);
		GL11.glDisable(GL11.GL_DEPTH_TEST);
		GL11.glRotatef(r.getRotation(), 0.0f,0.0f, 1.0f);
		
		GL11.glEnable(GL11.GL_BLEND);
		GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE);
		
		//GL11.glDisable(GL11.GL_COLOR_MATERIAL);
		GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);		
		GL11.glColor4f(r.getColor()[0], r.getColor()[1], r.getColor()[2],r.getOpacity());
		// start quad..
		GL11.glBegin(GL11.GL_QUADS);		
		
		GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex2f(0.0f,size.y);		
		GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex2f(0.0f,0.0f);				
		GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex2f(size.x,0.0f);			
		GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex2f(size.x,size.y);	
						
		GL11.glEnd();
		// done, pops!
		GL11.glPopMatrix();	}


	
	@Override
	public int getTexture() {
		return texture;
	}

}
